<?php
/**
 * Validator
 * - Only validate the content of text and textarea fields
 * - Check the type of data, return true if correct, return false otherwise
 * @author Do Truong Giang
 */
class Validator {
	/**
	 * isEmail
	 * - Return true if string $data is an email address format
	 * - Otherwise, return false
	 */
	public static function isEmail($data) {
		if ($data == "") {
			return true;
		}
		if (preg_match("/^[a-zA-Z0-9_\.]+@[a-zA-z0-9_]+\.[a-zA-Z0-9_\.]+$/", $data)) {
			return true;
		}
		return false;
	}
	/**
	 * isUrl
	 * - Return true if string $data is an URL format
	 * - Otherwise, return false
	 */
	public static function isUrl($data) {
		if ($data == "") {
			return true;
		}
		$urlregex = "^(https?)\:\/\/([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?@)?[a-z0-9+\$_-]+(\.[a-z0-9+\$_-]+)*(\:[0-9]{2,5})?(\/([a-z0-9+\$_-]\.?)+)*\/?(\?[a-z+&\$_.-][a-z0-9;:@/&%=+\$_.-]*)?(#[a-z_.-][a-z0-9+\$_.-]*)?\$"; 
		if (eregi($urlregex, $data)) {
			return true;	
		}  
		return false;
	}
	/**
	 * isPostCode
	 * - Return true if string $data is a post code format in JP
	 * - Otherwise, return false
	 */
	public static function isPostCode($data) {
		return true;
	}
	/**
	 * isTelOrFax
	 * - Return true if $data is telephone number of fax number
	 * - Otherwise, return false
	 */
	public static function isTelOrFax($data) {
		if ($data == "") {
			return true;
		}
		if (preg_match("/^[0-9\-]+$/", $data)) {
			return true;
		}
		return false;
	}
	/**
	 * isNumber
	 * - Return true if string $data is a number format
	 * - Otherwise, return false
	 */
	public static function isNumber($data) {
		if ($data == "") {
			return true;
		}
		if (preg_match("/^-{0,1}[0-9]*\.{0,1}[0-9]*$/", $data)) {
			return true;
		}
		return false;
	}
	/**
	 * isInteger
	 * - Return true if string $data is an integer format
	 * - Otherwise, return false
	 */
	public static function isInteger($data) {
		if ($data == "") {
			return true;
		}
		if (preg_match("/^-{0,1}[0-9]*$/", $data)) {
			return true;
		}
		return false;
	}
	/**
	 * isWord
	 * - Return true if string $data is a continuous word
	 * - Otherwise, return false
	 */
	public static function isWord($data) {
		if ($data == "") {
			return true;
		}
		if (preg_match("/^[a-zA-Z0-9_@\.\+\-]+$/", $data)) {
			return true;
		}
		return false;
	}
	
	/**
	 * isCheckdate
	 * - validate date insert
	 * @param String $stringDate : mm/dd/yy
	 * @author Nguyen Thanh Quyen
	 * @since 1.0 - Jul 21, 2009
	 */
	public static function isCheckdate($data) {
		if($data == "") {
			return true;
		}
		$arr = split("-",$data); // splitting the array
		$yy = $arr[0]; // first element of the array is month
		$mm = $arr[1]; // second element is date
		$dd = $arr[2]; // third element is year
		if(!checkdate($mm,$dd,$yy)) {
			return false;
		}else {
			return true;
		} 
	}
	/*Expression ^[0-9]+$

	^ :-Indicates the beginning of the string
	
	[0-9]+ :- indicates that the string begins with a number between (0-9) and is followed by numbers.
	
	$ :- Indicates the end of the String
	 */
}
?>